home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / unix / arcunx11 / arc.sh2 / arcmatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-01  |  3.5 KB  |  173 lines

  1. /*
  2.  *    arcmatch.c    1.1
  3.  *
  4.  *    Author: Thom Henderson
  5.  *    Original System V port: Mike Stump
  6.  *    Enhancements, Bug fixes, and cleanup: Chris Seaman
  7.  *    Date: Fri Mar 20 09:57:02 1987
  8.  *    Last Mod.    3/22/87
  9.  *
  10.  */
  11.  
  12. /*
  13.  * ARC - Archive utility - ARCMATCH
  14.  * 
  15.  * Version 2.17, created on 12/17/85) at 20:32:18
  16.  * 
  17.  * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  18.  * 
  19.  *     Description:
  20.  *          This file contains service routines needed to maintain an archive.
  21.  */
  22.  
  23. #include "arc.h"
  24. #include <sys/types.h>
  25. #include <sys/dir.h>
  26.  
  27. #define ASTERISK '*'        /* The '*' metacharacter */
  28. #define QUESTION '?'        /* The '?' metacharacter */
  29. #define BACK_SLASH '\\'         /* The '\' metacharacter */
  30. #define LEFT_BRACKET '['    /* The '[' metacharacter */
  31. #define RIGHT_BRACKET ']'    /* The ']' metacharacter */
  32.  
  33. #define IS_OCTAL(ch) (ch >= '0' && ch <= '7')
  34.  
  35. typedef INT BOOLEAN;
  36. #define VOID short
  37. #define TRUE 1
  38. #define FALSE 0
  39. #define EOS '\000'
  40.  
  41. static BOOLEAN do_list();
  42. static char nextch();
  43. static VOID list_parse();
  44.  
  45. int match(string, pattern)
  46. char *string;
  47. char *pattern;
  48. {
  49.     register int ismatch;
  50.  
  51.     ismatch = FALSE;
  52.     switch (*pattern)
  53.     {
  54.     case ASTERISK:
  55.         pattern++;
  56.         do
  57.         {
  58.             ismatch = match (string, pattern);
  59.         }
  60.         while (!ismatch && *string++ != EOS);
  61.         break;
  62.     case QUESTION:
  63.         if (*string != EOS)
  64.             ismatch = match (++string, ++pattern);
  65.         break;
  66.     case EOS:
  67.         if (*string == EOS)
  68.             ismatch = TRUE;
  69.         break;
  70.     case LEFT_BRACKET:
  71.         if (*string != EOS)
  72.             ismatch = do_list (string, pattern);
  73.         break;
  74.     case BACK_SLASH:
  75.         pattern++;
  76.     default:
  77.         if (*string == *pattern)
  78.         {
  79.             string++;
  80.             pattern++;
  81.             ismatch = match (string, pattern);
  82.         }
  83.         else
  84.             ismatch = FALSE;
  85.         break;
  86.     }
  87.     return(ismatch);
  88. }
  89.  
  90. static BOOLEAN do_list (string, pattern)
  91. register char *string;
  92. char *pattern;
  93. {
  94.     register BOOLEAN ismatch;
  95.     register BOOLEAN if_found;
  96.     register BOOLEAN if_not_found;
  97.     auto char lower;
  98.     auto char upper;
  99.  
  100.     pattern++;
  101.     if (*pattern == '!')
  102.     {
  103.         if_found = FALSE;
  104.         if_not_found = TRUE;
  105.         pattern++;
  106.     }
  107.     else
  108.     {
  109.         if_found = TRUE;
  110.         if_not_found = FALSE;
  111.     }
  112.     ismatch = if_not_found;
  113.     while (*pattern != ']' && *pattern != EOS)
  114.     {
  115.         list_parse(&pattern, &lower, &upper);
  116.         if (*string >= lower && *string <= upper)
  117.         {
  118.             ismatch = if_found;
  119.             while (*pattern != ']' && *pattern != EOS) pattern++;
  120.         }
  121.     }
  122.  
  123.     if (*pattern++ != ']')
  124.         abort("Character class error");
  125.     else
  126.         if (ismatch)
  127.             ismatch = match (++string, pattern);
  128.  
  129.     return(ismatch);
  130. }
  131.  
  132. static VOID list_parse (patp, lowp, highp)
  133. char **patp;
  134. char *lowp;
  135. char *highp;
  136. {
  137.     *lowp = nextch (patp);
  138.     if (**patp == '-')
  139.     {
  140.         (*patp)++;
  141.         *highp = nextch(patp);
  142.     }
  143.     else
  144.         *highp = *lowp;
  145. }
  146.  
  147. static char nextch (patp)
  148. char **patp;
  149. {
  150.     register char ch;
  151.     register char chsum;
  152.     register INT count;
  153.  
  154.     ch = *(*patp)++;
  155.     if (ch == '\\')
  156.     {
  157.         ch = *(*patp)++;
  158.         if (IS_OCTAL (ch))
  159.         {
  160.             chsum = 0;
  161.             for (count = 0; count < 3 && IS_OCTAL (ch); count++)
  162.             {
  163.                 chsum *= 8;
  164.                 chsum += ch - '0';
  165.                 ch = *(*patp)++;
  166.             }
  167.             (*patp)--;
  168.             ch = chsum;
  169.         }
  170.     }
  171.     return(ch);
  172. }
  173.